const Controller = require('egg').Controller;
const queryString = require('query-string');

class MemberController extends Controller {
    async create() {
      const body = this.ctx.request.body;
      delete body._csrf;
      console.log(body);
      if (body.orgId != '')
        body.orgId = Number(body.orgId);
  
      try {
        this.ctx.validate({
          orgId: { type: 'int' },
          netName: { type: 'string' },
        }, body);
      } catch (err) {
        var message = '';
        err
          .errors
          .map(function (item) {
            message += item.field + ' ' + item.message + '&#10';
          });
  
        const location = '/page/message?' + queryString.stringify({
          status: "warn",
          title: "学员记录失败",
          message: message,
          backTitle: '重新记录',
          backPage: "/page/memberCreate"
        }
        );
  
        this.ctx.body = { location };
  
        return;
      }
  
      const result = await this.ctx.service.member.create(body);
      const information = result.affectedRows === 1 ?
        {
          status: "success",
          title: "记录成功",
          message: '学员记录成功',
          backTitle: '继续记录',
          backPage: "/page/memberCreate"
        } :
        {
          status: "warn",
          title: '记录失败',
          message: '学员记录失败',
          backTitle: '重新记录',
          backPage: "/page/memberCreate"
        };
      const location = '/page/message?' + queryString.stringify(information);
  
      this.ctx.body = { location };
    }

    async update() {
      const body = this.ctx.request.body;
      const { backPage } = body;
      delete body._orgName;
      delete body._csrf;
      delete body.backPage;
  
      if (body.id != '')
        body.id = Number(body.id);
      if (body.orgId != '')
        body.orgId = Number(body.orgId);
  
      try {
        this.ctx.validate({
          id: { type: 'int' },
          orgId: { type: 'int' },
          netName: { type: 'string' },
        }, body);
      } catch (err) {
        var message = '';
        err
          .errors
          .map(function (item) {
            message += item.field + ' ' + item.message + '&#10';
          });
  
        const location = '/page/message?' + queryString.stringify({
          status: "warn",
          title: "学员记录修改失败",
          message: message,
          backTitle: '返回学员列表',
          backPage: "/page/" + backPage
        }
        );
  
        this.ctx.body = { location };
  
        return;
      }
  
      const result = await this.ctx.service.member.update(body);
  
      const information = result.affectedRows === 1 ?
        {
          status: "success",
          title: "修改成功",
          message: '学员记录修改成功',
          backTitle: '返回学员列表',
          backPage: "/page/" + backPage
        } :
        {
          status: "warn",
          title: '修改失败',
          message: '学员修改删除失败，点击返回学员列表',
          backTitle: '返回学员列表',
          backPage: "/page/" + backPage
        };
      const location = '/page/message?' + queryString.stringify(information);
  
      this.ctx.body = { location };
  
    }

}

module.exports = MemberController;